﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Kakeibo.Model {
    class KakeiboDatabaseConverter {
        public static bool ImportKokomaneDb(KakeiboDatabase db, string path){
            var categoryHash = new System.Collections.Hashtable();
            categoryHash["食費"] = "食費";
            categoryHash["生活用品"] = "生活用品";
            categoryHash["水道・光熱費"] = "通信・光熱費";
            categoryHash["ファッション"] = "美容・ファッション";
            categoryHash["美容・健康"] = "美容・ファッション";
            categoryHash["交通費"] = "交通費";
            categoryHash["医療費"] = "医療費";
            categoryHash["通信費"] = "通信・光熱費";
            categoryHash["育児・教育費"] = "育児・教育費";
            categoryHash["趣味・娯楽費"] = "趣味・娯楽費";
            categoryHash["交際費"] = "交際費";
            categoryHash["税金・保険"] = "税金・保険";
            categoryHash["特別出費"] = "特別出費";
            categoryHash["その他の支出"] = "その他";
            categoryHash["給与収入"] = "収入";
            categoryHash["口座振替"] = "口座";

            using (var sr = new StreamReader(path, Encoding.GetEncoding("Shift_JIS"))) {
                sr.ReadLine(); // read header
                string line = "";
                while ((line = sr.ReadLine()) != null) {
                    var cols = line.Replace("\"", "").Split(new char[] { ',' });

                    DateTime date = DateTime.Parse(cols[0].Substring(0, 10));

                    string debtorName = (string)categoryHash[cols[4]];
                    if (debtorName == null) {
                        debtorName = "その他";
                    }
                    string creditorName = "口座";
                    int amount = int.Parse(cols[11]) * int.Parse(cols[12]);
                    db.AddNewEntry(debtorName, "", creditorName, "", amount, date);
                }
            }

            return true;
        }
    }
}
